/**
 * Created by TX on 2017/5/19.
 */
var gulp = require('gulp');
var cache = require('gulp-cache');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var inject = require('gulp-inject');
var copy = require('gulp-copy');
var clean = require('gulp-clean');

var dataConfig = require('../config/data');
var stateMachineConfig = require('../config/state-machine-data');

var dist = {};//发布版本
/**
 * 压缩合并js
 * @author jw
 * @date 2016-11-08
 */
dist.concatJsFun = function (arr, name, cwd, dest) {
  cwd = cwd || 'src/dcv';
  dest = dest || 'dist/js';
  return gulp.src(arr, {read: true, cwd: cwd})
  //    .pipe(sourcemaps.init())
    .pipe(cache(
      uglify({
        compress: {
          drop_debugger: false//忽略debugger
        }
      }), {
        fileCache: dataConfig.fileCache,
        name: 'concat'
      })
    )
    .pipe(concat(name))
    //    .pipe(sourcemaps.write("."))
    .pipe(gulp.dest(dest));
};

gulp.task('concat:stateMachine-core', function () {
  return dist.concatJsFun(stateMachineConfig.stateMachineCoreJsArr, 'stateMachineCore.min.js', 'src/dcv/javascripts/stateMachine_Source', 'src/dcv/javascripts/stateMachine/minjs');
});
gulp.task('concat:stateMachine-ide', function () {
  return dist.concatJsFun(stateMachineConfig.stateMachineIdeJsArr, 'stateMachineIde.min.js', 'src/dcv/javascripts', 'src/dcv/javascripts/stateMachine/minjs');
});
gulp.task('concat:stateMachine-js-for-node', function () {
  return dist.concatJsFun(stateMachineConfig.stateMachineForNodeJsArr, 'stateMachineForNode.min.js', 'src/dcv/javascripts', 'src/dcv/javascripts/stateMachine/minjs');
});
gulp.task('concat:stateMachine-register', function () {
  return dist.concatJsFun(stateMachineConfig.stateMachineRegistersArr, 'stateMachineRegister.min.js', 'src/dcv/javascripts/stateMachine', 'src/dcv/javascripts/stateMachine/minjs');
});
gulp.task('concat:stateMachine-extend', function () {
  return dist.concatJsFun(stateMachineConfig.stateMachineExtendJsArr, 'stateMachineExtend.min.js', 'src/dcv/javascripts/stateMachine', 'src/dcv/javascripts/stateMachine/minjs');
});
gulp.task('copy:stateMachineHtml', function () {
  return gulp.src(['index.html', 'singleIndex.html'], {cwd: 'src/dcv/javascripts/stateMachine_Source/'})
    .pipe(gulp.dest('./', {cwd: 'src/dcv/javascripts/stateMachine/'}));
});
gulp.task('clean:stateMachine_StaticResource', function () {
  var targetWorkDir = 'src/dcv/javascripts/stateMachine/';
  return gulp.src(['css', 'editors', 'images', 'plug'], {cwd: targetWorkDir}).pipe(clean({force: false}));
});
gulp.task('copy:stateMachine_StaticResource', function () {
  var targetWorkDir = 'src/dcv/javascripts/stateMachine/';
  var sourceWorkDir = 'src/dcv/javascripts/stateMachine_Source/';
  return gulp.src(['css/**/*', 'editors/**/*', 'images/**/*', 'plug/**/*'], {cwd: sourceWorkDir})
    .pipe(copy(targetWorkDir)).pipe(gulp.dest('/**/*', {cwd: targetWorkDir}));
});
gulp.task('inject:release-stateMachine', function () {
  var targetHtml = gulp.src('index.html', {cwd: 'src/dcv/javascripts/stateMachine/'});
  var stateMachineIdeMinJs = gulp.src('stateMachineIde.min.js', {cwd: 'src/dcv/javascripts/stateMachine/minjs/'});
  var stateMachineRegisterMinJs = gulp.src('stateMachineRegister.min.js', {cwd: 'src/dcv/javascripts/stateMachine/minjs/'});

  var targetSingleHtml = gulp.src('singleIndex.html', {cwd: 'src/dcv/javascripts/stateMachine/'});
  var stateMachineForNodeMinJs = gulp.src('stateMachineForNode.min.js', {cwd: 'src/dcv/javascripts/stateMachine/minjs/'});
  var rv1 = targetSingleHtml
    .pipe(inject(stateMachineForNodeMinJs, {
      relative: true,
      starttag: '<!-- start:stateMachineForNodeJs -->',
      endtag: '<!-- end:stateMachineForNodeJs -->'
    }))
    .pipe(gulp.dest('.', {cwd: 'src/dcv/javascripts/stateMachine/'}));
  var rv2 = targetHtml
    .pipe(inject(stateMachineIdeMinJs, {
      relative: true,
      starttag: '<!-- start:stateMachineJs -->',
      endtag: '<!-- end:stateMachineJs -->'
    }))
    .pipe(inject(stateMachineRegisterMinJs, {
      relative: true,
      starttag: '<!-- start:stateMachineRegister -->',
      endtag: '<!-- end:stateMachineRegister -->'
    }))
    .pipe(gulp.dest('.', {cwd: 'src/dcv/javascripts/stateMachine/'}));
  return (rv1 && rv2);
});
/*gulp.task("clean:stateMachineMap", function(){
  targetWorkDir = "src/dcv/javascripts/stateMachine/minjs/";
  return gulp.src(["stateMachineCore.min.js.map", "stateMachineExtend.min.js.map", "stateMachineIde.min.js.map"], {cwd:targetWorkDir}).
    pipe(clean({force:true}));
});*/
gulp.task('build-stateMachine',
  gulp.series(
    gulp.parallel('clean:stateMachine_StaticResource'),
    gulp.parallel('copy:stateMachine_StaticResource'),
    gulp.parallel('concat:stateMachine-core', 'concat:stateMachine-ide', 'concat:stateMachine-register', 'concat:stateMachine-extend', 'concat:stateMachine-js-for-node'),
//    gulp.parallel("clean:stateMachineMap"),
    gulp.parallel('copy:stateMachineHtml'),
    gulp.parallel('inject:release-stateMachine')
  )
);
